import os

# coding=UTF-8
import xlrd
import xlwt  # 导入模块
from docx import Document
from docx.shared import Inches
from xlutils.copy import copy
from xlwt import XFStyle

import DateTime
import MkdirC
import Mongo

qxname=DateTime.DateTime().qxname()
year=DateTime.DateTime().MkdirYear()
month=DateTime.DateTime().MkdirMonth()
MkdirC.MkdirC().createFile("D:/"+year)
MkdirC.MkdirC().createFile("D:/"+year+"/"+month)

qx=Mongo.Mongo("qx",qxname)
count=qx.collCount()
jsb=Mongo.Mongo("JSB","jsb")

""""""
def creatMkdirC():
   for n in range(1, count + 1):
      code = qx.finddict({"_id": n})["code"]
      address = qx.finddict({"_id": n})["dizhi"]
      strname = "(" + str(n) + ")" + code + address  # 文件夹名称
      foldname = "D:/" + year + "/" + month + "/" + strname  # 文件夹所在D盘的位置
      foldnamezb = "D:/" + year + "/" + month + "/总表"   # 文件夹所在D盘的位置
      MkdirC.MkdirC().createFile(foldname)  # 创建该文件夹
      MkdirC.MkdirC().createFile(foldnamezb)  # 创建该文件夹
      MkdirC.MkdirC().copyFile("./excel/抢修施工图moban.dwg",foldname+'/抢修施工图.dwg')  # 创建该文件夹


def createFM():
   for n in range(1, count + 1):
      document = Document('./excel/竣工文件moban.docx')
      code= qx.finddict({"_id":n})["code"]
      address=qx.finddict({"_id":n})["dizhi"]
      leixing = qx.finddict({"_id": n})["leixing"]
      time = qx.finddict({"_id": n})["shijian"]
      person = qx.finddict({"_id": n})["qiangxiuren"]

      strname="("+str(n)+")"+code+address  #文件夹名称
      foldname="D:/"+year+"/"+month+"/"+strname   #文件夹所在D盘的位置
      foldname="D:/"+year+"/"+month+"/"+strname   #文件夹所在D盘的位置
      # MkdirC.MkdirC().createFile(foldname)   #创建该文件夹

      """以下部分为添加封面文件"""
      def read_document():
         for paragraph in document.paragraphs:
            for run in paragraph.runs:
               if "replacename" in run.text:
                  run.text = run.text.replace('replacename', leixing)
               elif "replaceaddress" in run.text:
                  run.text = run.text.replace('replaceaddress', address)
               elif "replacecode" in run.text:
                  run.text = run.text.replace('replacecode', code)
               elif "replacedate" in run.text:
                  run.text = run.text.replace('replacedate', time)
               elif "replaceperson" in run.text:
                  run.text = run.text.replace('replaceperson', person)

      """以上部分为添加封面文件"""

      read_document()
      try:
         document.save(foldname + '/竣工文件.docx')
         """以下部分为插图"""
         document2 = Document(foldname + '/竣工文件.docx')
         for filname in os.listdir(foldname):
            if filname.endswith('jpg'):
               document2.add_picture(foldname + "/" + filname, width=Inches(2.8))
         document2.save(foldname + '/竣工文件.docx')
         print(foldname + '/竣工文件.docx' + "创建成功")
         """以上部分为插图"""
      except:print("封面已存在")

      """删除所有封面"""
      # os.remove(foldname+'/竣工文件.docx')# 移除文件
      """删除所有封面"""


def createCLB():
   """该部分为三种格式"""
   style = XFStyle()  # 赋值style为XFStyle()，初始化样式
   styletop = XFStyle()  # 赋值style为XFStyle()，初始化样式

   font = xlwt.Font()
   font.name = "宋体"
   font.bold = False  # 是否加粗
   font.height = 20 * 10
   style.font = font

   font1 = xlwt.Font()
   font1.name = "黑体"
   font1.bold = False  # 是否加粗
   font1.height = 20 * 11
   styletop.font = font1

   borders = xlwt.Borders()
   borders.top = 1
   borders.bottom = 1
   borders.left = 1
   borders.right = 1
   style.borders = borders

   borders1 = xlwt.Borders()
   borders1.top = 1
   borders1.bottom = 1
   borders1.left = 1
   borders1.right = 0
   styletop.borders = borders1

   alignment = xlwt.Alignment()
   alignment.horz = 0x02
   alignment.vert = 0x01
   alignment.wrap = 1
   style.alignment = alignment

   alignment1 = xlwt.Alignment()
   alignment1.horz = 0x01
   alignment1.vert = 0x01
   alignment1.wrap = 0
   styletop.alignment = alignment1

   """以上部分为三种格式"""
   for n in range(1, count + 1):
      # document = Document('../excel/项目材料工程量表moban.xlsx')
      code= qx.finddict({"_id":n})["code"]
      address=qx.finddict({"_id":n})["dizhi"]
      leixing = qx.finddict({"_id": n})["leixing"]

      time = qx.finddict({"_id": n})["shijian"]
      person = qx.finddict({"_id": n})["qiangxiuren"]
      woakload = qx.finddict({"_id": n})["workload"]##获取工作量
      material = qx.finddict({"_id": n})["material"]###获取材料
      prince = qx.finddict({"_id": n})["jiage"]

      strname="("+str(n)+")"+code+address  #文件夹名称
      foldname="D:/"+year+"/"+month+"/"+strname  #文件夹盘所在D盘的位置)
      """
      11 项目名称
      21 项目编号
      31 项目地址

      材料表
         51 名称
         52 规格
         53 单位
         54 领料
         55 用料数

      工作量表
         50 分类编码
         51 工作内容
         52 说明
         53 规格
         54 单位
         55 工作量数
      """


      rb = xlrd.open_workbook('./excel/项目材料工程量表moban.xls',formatting_info=True)    #打开weng.xls文件
      wb = copy(rb)                          #利用xlutils.copy下的copy函数复制
      ws = wb.get_sheet(0) #打开材料表
      ws.write(1, 1, address+leixing,styletop)
      ws.write(2, 1, code,styletop)
      ws.write(3, 1, address,styletop)
      ws1 = wb.get_sheet(1) #工作量表
      ws1.write(1, 1, address+leixing,styletop)
      ws1.write(2, 1, code,styletop)
      ws1.write(3, 1, address,styletop)

      l=len(woakload)
      for nnn in range(0,l):
         id=woakload[nnn]["JSid"]
         unit=woakload[nnn]['unit']


         jsbf=jsb.finddict({"_id":id})
         neirong =jsbf["neirong"]  #抢修内容
         shuoming =jsbf["shuoming"]  #抢修说明
         guige =jsbf["guige"]  #抢修规格
         danwei =jsbf["danwei"]  #抢修单位
         jiage =jsbf["jiage"]  #抢修价格
         beizhu = jsbf["beizhu"]  # 备注是否双包

         "50分类编码  51工作内容  52说明  53规格  54单位  55作量数"
         ws1.write(5+nnn, 0, id, style)
         ws1.write(5+nnn, 1, neirong, style)
         ws1.write(5+nnn, 2, shuoming, style)
         ws1.write(5+nnn, 3, guige, style)
         ws1.write(5+nnn, 4, danwei, style)
         ws1.write(5+nnn, 5, unit, style)
         ws1.write(5+nnn, 6, jiage, style)
         ws1.write(5+nnn, 7, unit*jiage, style)
         ws1.write(5+nnn, 9, beizhu, style)
         ws1.write(35, 8, round(prince,2))

      ll=len(material)
      if not ll:pass
      else:
         for nn in range(0,ll):
            material1=material[nn]["material"]
            unit=material[nn]['unit']
            # danwei = material[nn]['danwei']

            "51名称 52规格 53单位  54领料 55用料数"
            ws.write(5 + nn, 1, material1, style)
            # ws.write(5 + nn, 3, danwei, style)
            ws.write(5 + nn, 4, unit, style)
            ws.write(5 + nn, 5, unit, style)
      try:
         wb.save(foldname+"/材料工作量表.xls")
         print(foldname + '/材料工作量表.xls' + "插入成功")
      except:print(("材料工作量表已存在"))#保存文件
      """删除所有封面"""
      # os.remove(foldname+"/材料工作量表.xls")# 移除文件
      """删除所有封面"""
